using System;
using System.Collections.Generic;
using Gtk;
using Gdk;

using Subsync.CLI;
using Subsync.Core.Sync;

namespace Subsync.GUI
{
	/// <summary>
	/// Author: Steven Goh Jian Wen with some modifications by Tsz Lai, Ruchi
	/// </summary>
	public partial class AdvanceTop : Gtk.Window
	{
        //Class Variables
		private GlossyRoundedFrame body_frame;
        private Dictionary<Pages, Gtk.Bin> pages = new Dictionary<Pages, Gtk.Bin>();
        private Gtk.Bin default_page;
        private Gtk.Bin current_page;
        private BannerButtons current_tab;
        private Dictionary<Pages, BannerButtons> tabs = new Dictionary<Pages, BannerButtons>();

        private string image_storage_path = System.IO.Path.Combine(new Config().GetProperty("ImageStoragePath"), "advancetop");

        //Constructor
		public AdvanceTop () : base(Gtk.WindowType.Toplevel)
		{
			this.Build ();
			
			// Adding a cheap border
			this.Remove (outermostv);
			Gtk.Frame border_frame = new Gtk.Frame();
			border_frame.Show ();
			border_frame.Add (outermostv);
			this.Add(border_frame);
			
			this.Resizable = false;
			this.WidthRequest = 800;

			//initiate banner (top)
			PopulateBanner ();
			
			//initiate body
			PopulateBody ();
			this.Icon = new Gdk.Pixbuf (System.IO.Path.Combine (new Config ().GetProperty ("ImageStoragePath"), "logo1.png"));
			this.DeleteEvent += HandleHandleDeleteEvent;
		}			
		
		void PopulateBanner ()
		{
			string banner_path = System.IO.Path.Combine (image_storage_path, "bannerimg");
			
			HBox bannerframe = new HBox ();
			bannerframe.BorderWidth = 0;
			bannerframe.Spacing = 0;
			
			Gtk.Image logo = new Gtk.Image (System.IO.Path.Combine (banner_path, "logo.png"));
			logo.WidthRequest = 270;
			logo.Show ();
			bannerframe.Add (logo);
			
			BannerButtons bannew = new BannerButtons (0, true, 43);
			tabs[Pages.AddMenu] = bannew;
			bannew.Show ();
			bannerframe.Add (bannew);
			current_tab = bannew;
			
			BannerButtons banset = new BannerButtons (1, false, 66);
			tabs[Pages.SettingsMenu] = banset;
			banset.Show ();
			bannerframe.Add (banset);
			
			BannerButtons bansync = new BannerButtons (2, false, 60);
			bansync.Show ();
			tabs[Pages.SyncMenu] = bansync;
			
			AdvTopMinBtn banmin = new AdvTopMinBtn(0,28);
			banmin.Show ();
			AdvTopMinBtn banmainwin = new AdvTopMinBtn(1,28);
			banmainwin.Show ();
			AdvTopMinBtn banclose = new AdvTopMinBtn(2,28);;
			banclose.Show ();
			
			VBox shutsyncstack = new VBox ();
			HBox TopRightMenu = new HBox();
			shutsyncstack.Spacing = 0;
			shutsyncstack.BorderWidth = 0;
			shutsyncstack.Add (TopRightMenu);
			TopRightMenu.Add(banmin);
			TopRightMenu.Add(banmainwin);
			TopRightMenu.Add(banclose);
			TopRightMenu.Show();
			shutsyncstack.Add (bansync);
			shutsyncstack.Show ();
			
			Gtk.Alignment bannerrightalign = new Gtk.Alignment ((float)1, (float)0.5, (float)0, (float)0);
			bannerrightalign.Add (shutsyncstack);
			bannerrightalign.WidthRequest = 421;
			bannerrightalign.Show ();
			bannerframe.Add (bannerrightalign);
			
			bannerframe.Show ();
			outermostv.Add (bannerframe);
			
			//events
			banmin.Clicked += HandleBanminClicked;
			banmainwin.Clicked += HandleBanmainwinClicked;
			banclose.Clicked += HandleBancloseClicked;
			bansync.Clicked += HandleBansyncClicked;
			bannew.Clicked += HandleBannewClicked;
			banset.Clicked += HandleBansetClicked;
		}

		
		void HandleBanmainwinClicked (object sender, EventArgs e)
		{
			List<Sync> list_of_syncs = new Config ().GetAllSyncs ();
			
			if (list_of_syncs.Count>0){
				Subsync.CLI.Views.main = new MainWindow ();
				Subsync.CLI.Views.main.ShowAll();
				this.Destroy ();
				Views.advwin = null;
			}else{
				NotificationDialog notice = new NotificationDialog("You will need at least 1 created sync before going into Simple View");
				notice.Show ();
			}
		}

		void HandleBanminClicked (object sender, EventArgs e)
		{				
			OperatingSystem os = Environment.OSVersion;
			PlatformID pid = os.Platform;			

			if (Views.tray_minizable){

				if ((pid==PlatformID.Win32NT)||(pid==PlatformID.Win32Windows)){
					Views.advwin.Hide ();
				}else{
					Views.advwin.SkipTaskbarHint = true;
					Views.advwin.Iconify ();
				}
			}else{
				Views.advwin.Iconify ();
			}
		}
		
		void PopulateBody ()
		{
			//spawn pages
			SpawnPages();			
			
			body_frame = new GlossyRoundedFrame();
			body_frame.HeightRequest = 400;
			
			Gdk.Color bgcolour = new Gdk.Color(69,69,69);
			body_frame.FillColor = bgcolour;
			body_frame.FillAlpha = .9;
			body_frame.DrawFill = true;
			body_frame.Radius = 0;
			body_frame.GlossHeight = 0;
			
			body_frame.Add(default_page);
			
			body_frame.ShowAll();
			outermostv.Add(body_frame);
		}
		
		void SpawnPages ()
		{
			pages[Pages.AddMenu] = new AddMenuPage (this);
			pages[Pages.AddMenu].Show ();
			pages[Pages.NewSyncWizard] = new NewSyncWizard (this);
			pages[Pages.NewSyncWizard].Show ();
			pages[Pages.AddLocalFolderWizard] = new AddFolderWizard (this);
			pages[Pages.AddLocalFolderWizard].Show ();
			pages[Pages.SyncMenu] = new SyncMenu (this);
			pages[Pages.SyncMenu].Show ();
			pages[Pages.SettingsMenu] = new SettingsMenu();
			pages[Pages.SettingsMenu].Show ();
			pages[Pages.RemoteSyncWizard] = new RemoteSyncWizard(this);
			pages[Pages.RemoteSyncWizard].Show();
			//defaults
			default_page = pages[Pages.AddMenu];
			current_page = pages[Pages.AddMenu];
		}
		
		public void ChangePage (Pages s)
		{
			if (s == Pages.SyncMenu)
				((SyncMenu)pages[s]).PopulateList ();
			if (s == Pages.AddLocalFolderWizard)
				pages[s] = new AddFolderWizard (this);
			if (s == Pages.NewSyncWizard)
				pages[s] = new NewSyncWizard (this);
			if (s == Pages.RemoteSyncWizard)
				pages[s] = new RemoteSyncWizard (this);
			pages[s].Show ();
			
			body_frame.Remove (body_frame.Child);
			body_frame.Add (pages[s]);
			current_page = pages[s];
			
			//tab switching
			if ((s == Pages.SyncMenu) || (s == Pages.AddMenu) || (s == Pages.SettingsMenu)) {
				current_tab.ToggleSelected ();
				current_tab = tabs[s];
				current_tab.ToggleSelected ();
			}
		}
		
		//events
		void HandleBansyncClicked (object sender, EventArgs e)
		{
			this.ChangePage (Pages.SyncMenu);
			current_tab.ToggleSelected ();
			current_tab = tabs[Pages.SyncMenu];
			current_tab.ToggleSelected ();
		}
		
		void HandleBancloseClicked (object sender, EventArgs e)
		{
			Views.HandleShutdown ();			
			Application.Quit ();
		}
		
		void HandleBannewClicked (object sender, EventArgs e)
		{
			this.ChangePage (Pages.AddMenu);
			current_tab.ToggleSelected ();
			current_tab = tabs[Pages.AddMenu];
			current_tab.ToggleSelected ();
		}
		
		void HandleBansetClicked (object sender, EventArgs e)
		{
			this.ChangePage (Pages.SettingsMenu);
			current_tab.ToggleSelected ();
			current_tab = tabs[Pages.SettingsMenu];
			current_tab.ToggleSelected ();
		}
		
		void HandleHandleDeleteEvent (object o, DeleteEventArgs args)
		{
			Views.HandleShutdown ();
			Application.Quit ();
		}
		
	}
}
